package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.model.GetRoomList.RoomList;

public class HousingAdapter extends BaseAdapter {

	Context context;

	ArrayList<RoomList> list;

	int type = 0;

	/** 0加载全部 1加载3个（不足三个按最大值取） */
	int Exhibitiontype = 0;

	public HousingAdapter(Context context, ArrayList<RoomList> list) {
		this.context = context;
		this.list = list;
	}

	public void SetDateNotify(ArrayList<RoomList> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void SetType(int type) {
		this.type = type;
		notifyDataSetChanged();
	}

	public void SetExhibitiontype(int Exhibitiontype) {
		this.Exhibitiontype = Exhibitiontype;
		notifyDataSetChanged();
	}

	public int GetExhibitiontype() {
		return Exhibitiontype;
	}

	@Override
	public int getCount() {
		if (Exhibitiontype == 0) {
			return list.size();
		} else {
			if (list.size() >= 3) {
				return 3;
			} else {
				return list.size();
			}
		}
	}

	@Override
	public Object getItem(int position) {
		return list == null ? null : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHoudler v = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.attention_item, null);
			v = new ViewHoudler();
			v.content_text = (TextView) convertView.findViewById(R.id.content_text);
			v.count_text = (TextView) convertView.findViewById(R.id.count_text);
			v.delete_btn = (ImageView) convertView.findViewById(R.id.delete_btn);
			v.attention_item_lines = convertView.findViewById(R.id.attention_item_lines);
			convertView.setTag(v);
		} else {
			v = (ViewHoudler) convertView.getTag();
		}

		if ((list.size() - 1) == position) {
			v.attention_item_lines.setVisibility(View.VISIBLE);
		} else {
			v.attention_item_lines.setVisibility(View.GONE);
		}

		// v.count_text.setText("" + (position + 1));
		v.content_text.setText(list.get(position).getRoomName());
		if (type == 0) {
			v.delete_btn.setVisibility(View.VISIBLE);
		} else {
			v.delete_btn.setVisibility(View.INVISIBLE);
			v.attention_item_lines.setVisibility(View.GONE);
		}
		v.delete_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				list.remove(position);
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	public static class ViewHoudler {
		// 序号
		TextView count_text;
		// 内容
		TextView content_text;
		// 删除
		ImageView delete_btn;

		View attention_item_lines;
	}

}
